package com.treasure.service.commons.lang.util;


import com.treasure.service.helper.LoggerManager;
import com.treasure.service.helper.PictureHelper;
import com.treasure.service.helper.TLogger;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @ClassName    : FileUtil
* @author       : panxingwu
* @date         : 2018年6月9日 下午4:44:47
* @Description  : 文件工具
*/
public class FileUtil {
   private static TLogger log = LoggerManager.getLogger(FileUtil.class);

    private static final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2-1);

    public static String uploadMultipartFile(MultipartFile file,String savePath) {
		String tfinalName = "";
		try {
			String fileName = file.getOriginalFilename();
			String finalName = savePath + "/" +IdCreater.getLongId()+"-"+fileName;
			tfinalName = savePath + "/C" +IdCreater.getLongId()+"-"+fileName+".jpg";
			File dest = new File(finalName);
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdir();
			}
			file.transferTo(dest); // 保存文件
            threadPool.execute(new CompressTask(dest));
		} catch (Exception e) {
		    log.error("上传文件失败",e);
			e.printStackTrace();
		}
		return tfinalName;
	}

	public static String asynCompressImage(String filePath){
        File file = new File(filePath);
        CompressTask ct = new CompressTask(file);
        threadPool.execute(ct);
        return ct.getTargetFileName();
    }

	private static class CompressTask implements Runnable{
        private final File file;
        CompressTask(File file){
            this.file = file;
        }

        public String getTargetFileName(){
            return file.getName().substring(0,file.getName().lastIndexOf("."))+"_C.jpg";
        }
        @Override
        public void run() {
            if(file!=null && file.exists()){
                try {
                    log.info("Start to compress picture:{1}",file.toString());
                    FileInputStream fis = new FileInputStream(file);
                    ByteArrayOutputStream bos = PictureHelper.compressInS(fis);
                    File cfile = new File(file.getParent()+"/"+getTargetFileName());
                    FileOutputStream fos = new FileOutputStream(cfile);
                    bos.writeTo(fos);
                    bos.flush();
                    bos.close();
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    log.error("Compress picture error:"+file,e);
                }
            }
        }
    }

//    public static void main(String[] args){
//        File file = new File("/Users/echov/tmp/bigBg.png");
//        System.out.println(file.getParent()+"==="+file.getName());
//        CompressTask c = new CompressTask(file);
//        c.run();
//        System.out.println(file.getParent()+"==="+c.getTargetFileName());
//    }
}
